/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_pom_enq.c
    Module  : (geometric) itk module.

        This file manages some operations about enquiring the db by the sql syntax.
=======================================================================================================================
Date               Name              Description of Change
14-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#include <enq.h>
#include <pom.h>
#include <sample_err.h>

#include "tc_pom_enq.h"

// SQL > select count(t.targetProp) from revType t where targetProp = 'codeValue'
int GENQ_get_value_count( const char *className, const char *targetProp, const char *codeValue, int *count )
{
    int        ifail         = ITK_ok;

    void       ***report     = NULL;
    int        rows          = 0;
    int        cols          = 0;
    const char *qry          = "GENQ_get_value_count_sql";
    const char *count_exprid = "unique_exprid_1";
    const char *where_exprid = "unique_exprid_2";

    const char *str_sel_list[] = { count_exprid };

    *count = 0;

    CALL( POM_enquiry_create( qry ) );

    // Select count(xx)
    CALL( POM_enquiry_set_attr_expr ( qry, count_exprid, className, targetProp, POM_enquiry_count, "") );
    CALL( POM_enquiry_add_select_exprs(qry, 1, str_sel_list) );

    /*Create the expr targetProp = 'codeValue'.*/
    CALL( POM_enquiry_set_string_expr (qry, where_exprid, className, targetProp, POM_enquiry_equal, (char*)codeValue ) );

    // Where
    CALL( POM_enquiry_set_where_expr( qry, where_exprid ) );

    // Execute
    CALL( POM_enquiry_execute(qry, &rows, &cols, &report) );
    ifail = POM_enquiry_delete( qry );

    // return the result
    if (ifail == ITK_ok && rows != 0 && cols != 0)
    {
        *count = *((int*) report[0][0]);//DATA_string_to_int( (char*) report[0][0] );
    }

    MEM_free( report );

    return ifail;   
}
